<template>
  <div class="page_set page-miH page-bgBack">
    <a-form layout="inline" class="formrow" :form="searchForm" :colon="true">
      <a-form-item>
        <a-input-search v-model="title" style="width: 240px" placeholder="关键字搜索" :allow-clear="true" @search="searchContarct" />
      </a-form-item>
      <a-form-item style="margin-right: 0px">
        <a-button type="primary" icon="plus" @click="createNotice">新建通告</a-button>
      </a-form-item>
    </a-form>
    <a-row class="allNews">
      全部消息
      <span>（消息最多保存180天）</span>
    </a-row>
    <a-table
      :columns="noticeColumns"
      :data-source="noticelist"
      :pagination="pagination"
      row-key="id"
      :loading="loading"
      @change="tabChange"
    >
      <span slot="signNo" slot-scope="text, record">
        <span style="font-size: 12px">{{ record.signNo ? record.signNo : "--" }}</span>
      </span>
      <a slot="readStatus" slot-scope="text, record" @click="showDetail(record)">
        <span :class="text === 0 ? 'colorC0C0C0' : text === 1 ? 'tc-theme' : 'colorC0C0C0'">•</span>

        <span style="color: #595959" v-if="text === 0">共{{ record.readCount + record.noReadCount }}人未读{{ record.noReadCount }}人</span>
        <span style="color: #595959" v-if="text === 1">共{{ record.readCount + record.noReadCount }}人全部已读</span>
        <span style="color: #595959" v-if="text === 2">共{{ record.readCount + record.noReadCount }}人未读{{ record.noReadCount }}人</span>
      </a>
      <!-- <div slot="readEmpName" slot-scope="text" style="max-height:70px;overflow-y: auto;">
        {{ text }}
      </div>
      <div slot="noReadEmpName" slot-scope="text" style="max-height:70px;overflow-y: auto;">
        {{ text }}
      </div> -->
      <span slot="employeeName" slot-scope="text, record">
        {{
          record.partySignAccountList.filter((item) => {
            return item.type === 1;
          })[0] &&
          record.partySignAccountList.filter((item) => {
            return item.type === 1;
          })[0].name !== null
            ? record.partySignAccountList.filter((item) => {
                return item.type === 1;
              })[0].name
            : "--"
        }}
      </span>
      <span slot="action" slot-scope="text, record">
        <span style="display: flex; align-items: center">
          <a class="primary_color pointer" type="link" @click="goDetail(record)">详情</a>
        </span>
      </span>
    </a-table>
    <a-modal title="阅读状态" :visible="recordVisible" @ok="recordVisible = false" @cancel="recordVisible = false" width="750px">
      <div style="display: flex">
        <div style="width: 350px; color: #767885; font-size: 14px; height: 300px; margin-left: 8px; overflow: scroll">
          <span style="margin-left: 8px"
            >已读人员<span class="primary">{{ recordDetail.readCount }}人</span></span
          >
          <div v-if="recordDetail.readCount === 0 || !recordDetail.readCount"><a-empty style="margin-top: 40px" /></div>
          <div v-else>
            <div v-if="recordDetail.readEmpInformation" style="display: flex; flex-wrap: wrap">
              <div v-for="(item, index) in recordDetail.readEmpInformation" :key="index">
                <div style="width: 100px; height: 40px; margin-left: 8px; margin-top: 12px">
                  <a-avatar :src="item.avatar ? item.avatar : 'avatar.png'" :size="30" />
                  <span style="color: #767885; font-size: 14px; margin-left: 10px">{{ item.empName }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <a-divider type="vertical" style="height: 300px" />
        <div style="width: 350px; color: #767885; font-size: 14px; height: 300px; overflow: scroll">
          <span style="margin-left: 8px"
            >未读人员<span class="primary">{{ recordDetail.noReadCount }}人</span></span
          >
          <div v-if="recordDetail.noReadCount === 0 || !recordDetail.noReadCount"><a-empty style="margin-top: 40px" /></div>
          <div v-else>
            <div v-if="recordDetail.noReadEmpInformation" style="display: flex; flex-wrap: wrap">
              <div v-for="(item, index) in recordDetail.noReadEmpInformation" :key="index">
                <div style="width: 100px; height: 40px; margin-left: 8px; margin-top: 12px">
                  <a-avatar :src="item.avatar ? item.avatar : 'avatar.png'" :size="30" />
                  <span style="color: #767885; font-size: 14px; margin-left: 10px">{{ item.empName }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </a-modal>
    <!-- 预览通知 -->
    <a-modal
      title="预览"
      :confirm-loading="confirmLoading"
      :visible="detailVisible"
      class="notice-modal"
      width="800px"
      @ok="detailVisible = false"
      @cancel="detailVisible = false"
    >
      <div class="flex-start-center">
        <iframe :src="eleContractDetail.path" class="checkbox" style="width: 700px; height: 500px; overflow: auto" />
      </div>
    </a-modal>
    <!-- 删除通知 -->
    <a-modal
      title="删除通知"
      :visible="deleteVisible"
      class="notice-modal"
      width="468px"
      @ok="confirmDelete"
      @cancel="deleteVisible = false"
    >
      <div class="deleteline">
        <p><a-icon type="exclamation-circle" />确定删除这条通知？</p>
      </div>
    </a-modal>
  </div>
</template>
<script>
import { formatDateAll } from "@/utils/index";
import { ListEmpNotices, deleteUserNoticeBatch } from "@/api/company/index";
const noticeColumns = [
  {
    title: "通知标题",
    dataIndex: "title",
    scopedSlots: { customRender: "title" }
  },
  {
    title: "阅读状态",
    dataIndex: "readStatus",
    scopedSlots: { customRender: "readStatus" }
  },
  {
    title: "发布时间",
    dataIndex: "publishDate",
    customRender: (text, record, index) => {
      return formatDateAll(text);
    }
  },
  // {
  //   title: "已阅读人",
  //   dataIndex: "readEmpName",
  //   width: 250,
  //   scopedSlots: { customRender: "readEmpName" }
  // },
  // {
  //   title: "未阅读人",
  //   dataIndex: "noReadEmpName",
  //   width: 250,
  //   scopedSlots: { customRender: "noReadEmpName" }
  // },
  {
    title: "发布范围",
    dataIndex: "publishEmpRangeIsAll",
    width: 180,
    customRender: (text, record, index) => {
      return text === true ? "全公司" : "部分员工";
    }
  },
  {
    title: "推送设置",
    dataIndex: "noticeType",
    width: 200,
    customRender: (text, record, index) => {
      const content = [];

      if (text) {
        text.split(",").forEach((element) => {
          if (element === "2") {
            content.push("短信");
          } else if (element === "1") {
            content.push("微信");
          } else if (element === "3") {
            content.push("邮件");
          }
        });
      } else {
        content.push("--");
      }
      return content;
    }
  },
  {
    title: "操作",
    dataIndex: "action",
    scopedSlots: { customRender: "action" }
  }
];
export default {
  data() {
    return {
      loading: false,
      noticeColumns,
      searchForm: this.$form.createForm(this),
      title: null,
      relatedStaffName: "",
      noticelist: [],
      categorieslistAll: [],
      detailVisible: false,
      confirmLoading: false,
      deleteVisible: false,
      currentContract: {},
      eleContractDetail: {},
      recordDetail: {},
      recordVisible: false,
      currentTab: 1,
      companyId: "",
      status: [
        { id: 0, name: "待发布" },
        { id: 1, name: "已发布" },
        { id: 2, name: "已下线" }
      ],
      pagination: {
        pageSize: 10,
        total: 0,
        size: "default",
        current: 1,
        showTotal: (total) => `共 ${total} 条`,
        showQuickJumper: true
      }
    };
  },
  created() {
    this.relatedStaffName = this.$store.state.auth.name;
    this.companyId = this.$store.state.hr.companyId;
    this.getAllNoticelist();
  },
  methods: {
    showDetail(e) {
      console.log(e);
      this.recordDetail = e;
      this.recordVisible = true;
    },
    createNotice() {
      this.$router.push({
        path: `person/createNotice`,
        query: {
          sign: "0"
        }
      });
    },
    getAllNoticelist() {
      this.loading = true;
      ListEmpNotices({
        companyId: this.companyId,
        title: this.title,
        pageSize: this.pagination.pageSize,
        pageNo: this.pagination.current
      })
        .then((res) => {
          this.noticelist = res.data.entities;
          this.pagination.total = res.data.entityCount;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    tabChange(pagination, current) {
      this.pagination.current = pagination.current;

      this.getAllNoticelist();
    },
    goDetail(record) {
      this.$router.push({
        path: `person/detail`,
        query: {
          userId: record.publishUserId,
          empName: record.empName,
          sign: "1",
          id: record.id
        }
      });
    },
    searchContarct(e) {
      console.log("123", e);
      if (e) {
        this.pagination.current = 1;
        ListEmpNotices({
          companyId: this.companyId,
          title: e,
          pageSize: this.pagination.pageSize,
          pageNo: 1 // this.pagination.current
        }).then((res) => {
          this.noticelist = res.data.entities;
          this.pagination.total = res.data.entityCount;
        });
      } else {
        this.pagination.current = 1;
        this.getAllNoticelist();
      }
    },
    deleteNotice(record) {
      this.currentNotice = record;
      this.deleteVisible = true;
    },
    confirmDelete() {
      var userNoticeIds = [];
      userNoticeIds.push(this.currentNotice.id);
      deleteUserNoticeBatch(userNoticeIds).then((res) => {
        this.deleteVisible = false;
        this.$message.success("删除成功");
        this.getAllNoticelist();
      });
    }
  }
};
</script>
<style lang="less" scoped>
::v-deep.ant-modal-body {
  padding: 24px 12px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
}
.primary {
  color: var(--primary);
  cursor: pointer;
}
.formrow {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
}
.page_set {
  .checkbox {
    width: 600px;
    height: auto;
    overflow: auto;
  }
  .ht .ant-modal-body {
    height: 500px;
    overflow: auto;
    display: flex;
    justify-content: center;
  }
}
.allNews {
  font-size: 14px;
  color: rgba(34, 36, 38, 1);
  line-height: 40px;
  span {
    font-size: 14px;
    color: #aaaaaa;
    line-height: 40px;
  }
}
.deleteline {
  .anticon {
    font-size: 20px;
    line-height: 30px;
    color: #faad14;
    margin-left: 10px;
  }
  p {
    font-size: 16px;
    line-height: 30px;
    margin-top: 10px;
    text-align: center;
  }
}
</style>
